﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ToracTechnologies.ExpressionTree.Testing;
using ToracTechnologies.ExpressionTreeLibrary.DynamicApi.Framework;

namespace ToracTechnologies.ExpressionTree.Testing.DynamicApi
{

    [TestClass]
    public class DynamicApiExpressionBuilderUnitTest
    {

        #region String Based

        [TestMethod]
        public void Equals_StringBased_Test1()
        {
            var lst = Jason.CreateDummyList(100);

            var left = ParameterBuilder.BuildParameterFromStringName<Jason>("Id");

            var selector = ExpressionBuilder.BuildStatement<Jason, int>(left, ExpressionBuilder.DynamicUtilitiesEquations.Equal, 5);

            var result = lst.AsQueryable().Where(selector).ToArray();

            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("Test_5", result.ElementAt(0).txt);
        }

        #endregion

        #region Lambda Based

        [TestMethod]
        public void Equals_LambdaBased_Test1()
        {
            var lst = Jason.CreateDummyList(100);

            var left = ParameterBuilder.BuildParameterFromLinqPropertySelector<Jason>(x => x.Id);

            var selector = ExpressionBuilder.BuildStatement<Jason, int>(left, ExpressionBuilder.DynamicUtilitiesEquations.Equal, 5);

            var result = lst.AsQueryable().Where(selector).ToArray();

            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("Test_5", result.ElementAt(0).txt);
        }

        #endregion

        #region Contains

        [TestMethod]
        public void Contains_Test1()
        {
            var l = new List<int>() { 1, 2, 3 };

            var lst = Jason.CreateDummyList(100);
            var right = ParameterBuilder.BuildParameterFromLinqPropertySelector<Jason>(x => x.Id);
            var sql = ExpressionBuilder.BuildIEnumerableContains<Jason, int>(l, right);

            var result = lst.AsQueryable().Where(sql).OrderBy(x => x.Id).ToArray();

            Assert.AreEqual(3, result.Count());
            Assert.AreEqual(1, result.ElementAt(0).Id);
            Assert.AreEqual(2, result.ElementAt(1).Id);
            Assert.AreEqual(3, result.ElementAt(2).Id);
        }

        #endregion

    }
}


